package com.yycx.module.bus.provider.handler.haiying.tasktodo.calculate;

import cn.hutool.core.bean.BeanUtil;
import com.yycx.bpm.provider.common.FlowableHelp;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.module.MyExportParams;
import com.yycx.common.base.module.export.DictDataVo;
import com.yycx.common.base.module.export.ExportField;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.ExportInterceptor;
import com.yycx.common.mybatis.interceptor.PageInterceptor;
import com.yycx.common.mybatis.model.PageParams;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.poi.ExcelStyleUtil;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusDeptService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysBankService;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 我的已办任务订单明细列表
 *
 * @author zyf
 * @engity HyOrderDetails
 */
@Component
public class HyCalApproveTab3ListHandler implements PageInterceptor<TaskResponse>, ExportInterceptor {

    @Resource
    private HyOrderService orderService;

    @Resource
    private SysBankService bankService;

    @Resource
    private BaseUserService userService;

    @Resource
    private BusSalemanService salemanService;

    @Resource
    private BusDeptService deptService;

    @Resource
    private BusCustomService customService;

    @Resource
    private SysCompanyService companyService;


    @Autowired
    private FlowableHelp flowableHelp;


    @Override
    public void prepare(CriteriaQuery<TaskResponse> cq, PageParams pageParams, EntityMap params) {
        String beginDate = cq.getParams("beginDate");
        String endDate = cq.getParams("endDate");
        String customId = cq.getParams("customId");
        String orderNo = cq.getParams("orderNo");
        String saleManName = cq.getParams("saleManName");
        Long companyId = OpenHelper.getCompanyId();
        cq.select(HyOrder.class, "*");
        cq.addSelect(BusDept.class, "deptName", "deptType");
        cq.eq(BusDept.class, "deptType");
        cq.eq(HyOrder.class, "deptId");
        cq.eq(HyOrder.class, "companyId");
        if (FlymeUtils.isNotEmpty(orderNo)) {
            cq.and(e -> e.likeLeft("ho.orderNo", orderNo).or().likeLeft("ho.outOrderNo", orderNo));
        }
        if (FlymeUtils.isNotEmpty(beginDate)) {
            cq.ge("ho.sendDate", beginDate);
            cq.le("ho.sendDate", endDate);
        }
        if (FlymeUtils.isNotEmpty(saleManName)) {
            cq.like("ho.saleManName", saleManName);
        }
        cq.eq(FlymeUtils.isNotEmpty(customId), "customId", customId);
        cq.in(true, "ho.calculateState", 1, 2);
        cq.isNotNull("ho.applyIncId");
        String userName = OpenHelper.getUserName();
        Boolean isLd = OpenHelper.hasRole("ROLE_CW_JSBX", "ROLE_SW_SCZZ", "ROLE_SW_BMZJ", "ROLE_SW_SKY");
        //已审批中有我的记录
        if (!userName.contains("admin") && !isLd) {
            Long userId = OpenHelper.getUserId();
            cq.inSql(userId.toString(), "select USER_ID_ from act_hi_comment c where c.PROC_INST_ID_=ho.applyIncId and c.TYPE_='WC' and c.ACTION_ ='AddComment' and c.USER_ID_=" + userId);
        } else {
            if (FlymeUtils.isEmpty(companyId)) {
                companyId = OpenHelper.getCompanyId();
            }
            cq.eq(true, "ho.companyId", companyId);
        }
        cq.createJoin(BusDept.class);
        cq.orderByDesc("ho.orderNo");
    }

    @Override
    public List<EntityMap> complete(CriteriaQuery<TaskResponse> cq, List<EntityMap> result, EntityMap extra) {
        for (EntityMap entityMap : result) {
            String processInstanceId = entityMap.get("applyIncId");
            String businessKey = entityMap.get("orderId");
            String assignee = entityMap.get("assignee");
            HyOrder order = orderService.getById(businessKey);
            if (FlymeUtils.isNotEmpty(order)) {
                Map map = BeanUtil.beanToMap(order, false, true);
                entityMap.putAll(map);
                entityMap.put("takeDate", DateUtils.formatDate(order.getTakeDate()));
                entityMap.put("sendDate", DateUtils.formatDate(order.getSendDate()));
                entityMap.put("createTime", DateUtils.formatDate(order.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                EntityMap taskMap = flowableHelp.getTaskInfo(processInstanceId);
                if (FlymeUtils.isNotEmpty(taskMap)) {
                    entityMap.putAll(taskMap);
                }
                Long customId = order.getCustomId();
                BusCustom custom = customService.getById(customId);
                if (FlymeUtils.isNotEmpty(custom)) {
                    entityMap.put("customName", custom.getCustomName());
                }
                if (FlymeUtils.isNotEmpty(assignee)) {
                    BaseUser assigneeUser = userService.getById(Long.parseLong(assignee));
                    entityMap.put("assignee", assigneeUser.getNickName());
                }
                Long companyId = order.getCompanyId();
                SysCompany company = companyService.getById(companyId);
                entityMap.put("companyName", company.getCompanyName());
                Integer calculateState = order.getCalculateState();
                if (calculateState.equals(2)) {
                    entityMap.put("taskName", "已完成");
                }
            }
        }
        return result;
    }

    @Override
    public void initExportParams(MyExportParams exportParams) {

    }

    @Override
    public void initExcelExportEntity(ExportField exportField, List list) {
        Object key = exportField.getKey();
        if (key.equals("deptType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("自营");
            dictDataVo.setValue("ZY");
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("招商");
            dictDataVo2.setValue("ZS");
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
        if (key.equals("sendType")) {
            List<DictDataVo> dictDataVos = new ArrayList<>();
            DictDataVo dictDataVo = new DictDataVo();
            dictDataVo.setLabel("现款");
            dictDataVo.setValue(1);
            DictDataVo dictDataVo2 = new DictDataVo();
            dictDataVo2.setLabel("预付款");
            dictDataVo2.setValue(2);
            DictDataVo dictDataVo3 = new DictDataVo();
            dictDataVo3.setLabel("资保金");
            dictDataVo3.setValue(3);
            DictDataVo dictDataVo4 = new DictDataVo();
            dictDataVo4.setLabel("资信");
            dictDataVo4.setValue(4);
            dictDataVos.add(dictDataVo);
            dictDataVos.add(dictDataVo2);
            dictDataVos.add(dictDataVo3);
            dictDataVos.add(dictDataVo4);
            exportField.setDictData(dictDataVos);
            exportField.setReplace();
        }
    }
}
